<?php
namespace lib;

/**
 * 数据模型类的父类，所有的数据模型类要继承此类
 *
 * @package lib
 */
class Model
{
    private $pdo;
    private $table;
    private $pk;

    /**
     * model constructor.
     * @param $table 表名
     * @param $pk 主键名
     */
    public function __construct($table,$pk)
    {
        $this->table=$table;
        $this->pk=$pk;
        $config=require CONFIG_DIR.DIRECTORY_SEPARATOR.'database.php';
        $host=$config['host'];
        $dbname=$config['dbname'];
        $dsn="mysql:host=$host;dbname=$dbname";
        $this->pdo=new \PDO($dsn,$config['username'],$config['password']);
    }

    public function get(Array $data=[]){
        //select * from students where id=4 and name='小黄';
        $sets='';
        foreach($data as $k=>$v){
            $sets.=$k."='".$v."' and ";
        }
        $sets=rtrim($sets,'and ');

        if(empty($data)){
            $sql="select * from ".$this->table;
        }else{
            $sql="select * from ".$this->table." where ".$sets.";";
        }
        $res=$this->pdo->query($sql);
        $rows=$res->fetchAll(\PDO::FETCH_ASSOC);
        return  $rows;
    }

    /**
     * @param array $data 要操作的数据
     */
    public function save(Array $data){
        $sets='';
        foreach($data as $k=>$v){
            $sets.=$k."='".$v."',";
        }
        $sets=rtrim($sets,',');

        if(isset($data[$this->pk])){  //如果用户有传主键字段就为update
            $sql="update ".$this->table." set ".$sets." where ".$this->pk."=".$data[$this->pk].";";
        }else{
            $sql="insert into ".$this->table." set ".$sets.";";
        }
        $res=$this->pdo->exec($sql);
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function join(Array $data,Array $condition=[]){
       // SELECT * FROM `user` JOIN `comment` ON `user`.uid=`comment`.uid
        $joins='';
        $conditions='';
        foreach($data as $k=>$v){
            $joins.=' join '.$k.' on '.$this->table.'.'.$this->pk.'='.$k.'.'.$v;
        }
        if(!empty($condition)) {
            $where = ' where ';
            foreach ($condition as $k => $v) {
                $conditions .= $k . "='" . $v . "' and ";
            }
            $conditions = rtrim($conditions, 'and ');
            $conditions = $where . $conditions;
        }

        $sql="select * from ".$this->table.$joins.$conditions.";";
        //echo $sql;
        $res=$this->pdo->query($sql);
        $rows=$res->fetchAll(\PDO::FETCH_ASSOC);
        return  $rows;
    }

    public function delete(Array $data){
        $sets='';
        foreach($data as $k=>$v){
            $sets.=$k."='".$v."' and ";
        }
        $sets=rtrim($sets,'and ');
        $sql="delete from ".$this->table." where ".$sets.";";

        $res=$this->pdo->exec($sql);
        if($res){
            return true;
        }else{
            return false;
        }
    }

    public function input($name)
    {
        return htmlspecialchars(stripslashes($_REQUEST[$name]));
    }
}
